/**
 * 
 */
package reco.db.validation;

import static com.google.common.base.Preconditions.*;
import reco.db.Constraint;

/**
 * @author juanjo
 */
public class BetweenConstraint<T extends Comparable<T>> extends Constraint<T> {

    private final T max;
    private final T min;

    public BetweenConstraint(T minValue, T maxValue) {
        super("between", "isOutsideOf");
        checkNotNull(minValue, "minValue");
        checkNotNull(maxValue, "maxValue");
        checkArgument(minValue.compareTo(maxValue) != 0, "minValue should not be equal to maxValue");
        
        if (minValue.compareTo(maxValue) > 0) {
            this.min = maxValue;
            this.max = minValue;
        } else {
            this.min = minValue;
            this.max = maxValue;
        }
    }

    @Override
    public boolean validate(T value) {
        if (value == null) {
            return true;
        }
        return this.max.compareTo(value) >= 0 && this.min.compareTo(value) <= 0;
    }

    /**
     * @return the errorArguments
     */
    @Override
    public final Object[] getErrorArguments() {
        return new Object[] { this.min, this.max };
    }

}
